package com.cxmx.escort.controller;


import com.cxmx.escort.base.BaseController;
import com.cxmx.escort.base.MD5Utils;
import com.cxmx.escort.base.ResponseFormat;
import com.cxmx.escort.base.Result;
import com.cxmx.escort.baseEnum.VariableEnum;
import com.cxmx.escort.common.redis.RedisUtils;
import com.cxmx.escort.entity.BedOrder;
import com.cxmx.escort.entity.Malfunction;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * <p>
 * 故障申报 前端控制器
 * </p>
 *
 * @author 陈云轩
 * @since 2019-10-29
 */
@RestController
@RequestMapping("/malfunction")
@Validated
@Api(value = "故障申报", tags = {"反馈接口"})
@SuppressWarnings("unchecked")
public class MalfunctionController extends BaseController {
    @ApiOperation(value = "故障申报", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", dataType = "String", paramType = "header", required = true),
    })
    @PostMapping
    public Result<Boolean> addFeedBack(@RequestBody Malfunction malfunction, @NotNull(message = "token不可为空") @RequestHeader("token") String token) {
        malfunction.setUserId(String.valueOf(getUserId(token)));
        BedOrder bedOrder = bedOrderService.getById(malfunction.getOrderId());
        if (malfunction.getMafType() == 1) {
            Duration a = Duration.between(bedOrder.getLockTime(), LocalDateTime.now());
            if (a.toMinutes() > 10) {
                return ResponseFormat.retParam(70013, null);
            }
            bedOrder.setOrderStatus(3);
            bedOrder.setBackPrice(new BigDecimal(VariableEnum.THE_DEPOSIT.getCode() / 100));
            bedOrder.setUnlockTime(LocalDateTime.now());
            bedOrder.setUnpaidPrice(new BigDecimal(0));
            String refundId = String.valueOf(RedisUtils.get("bed:getRefundId:" + bedOrder.getUserId()));
            WxPayRefundRequest wxPayUnifiedOrderRequest = new WxPayRefundRequest();
            wxPayUnifiedOrderRequest.setRefundFee(VariableEnum.THE_DEPOSIT.getCode());
            wxPayUnifiedOrderRequest.setTotalFee(VariableEnum.THE_DEPOSIT.getCode());
            wxPayUnifiedOrderRequest.setRefundDesc("退还预付款");
            wxPayUnifiedOrderRequest.setOutTradeNo(refundId);
            wxPayUnifiedOrderRequest.setNotifyUrl("https://api.bochenkj.cn/pay/notify/refund");
            wxPayUnifiedOrderRequest.setRefundFeeType("CNY");
            wxPayUnifiedOrderRequest.setOutRefundNo(refundId);
            try {
                wxService.refund(wxPayUnifiedOrderRequest);
                RedisUtils.del("bed:getRefundId:" + bedOrder.getUserId());
                RedisUtils.del("bed:getUserId:" + bedOrder.getUserId());
            } catch (WxPayException e) {
                e.printStackTrace();
            }
        } else {
            bedOrder.setUnlockTime(LocalDateTime.now());
            BigDecimal bigDecimal = new BigDecimal(MD5Utils.getPrice(bedOrder.getLockTime(), bedOrder.getUnlockTime(), bedOrder.getUnitCost().doubleValue(), bedOrder.getUserId()));
            bedOrder.setPrice(bigDecimal);
            RedisUtils.del("bed:bed:" + bedOrder.getBedCode());
            double consumePrice = bedOrder.getPrice().doubleValue() * 100;
            int refundFee = VariableEnum.THE_DEPOSIT.getCode() - (int) consumePrice;
            //如果可以承担
            if (refundFee >= 0) {
                bedOrder.setOrderStatus(3);
                double back = (double) refundFee / 100;
                bedOrder.setBackPrice(new BigDecimal(back));
                bedOrder.setUnpaidPrice(new BigDecimal(0));
                String refundId = String.valueOf(RedisUtils.get("bed:getRefundId:" + bedOrder.getUserId()));
                WxPayRefundRequest wxPayUnifiedOrderRequest = new WxPayRefundRequest();
                wxPayUnifiedOrderRequest.setRefundFee(refundFee);
                wxPayUnifiedOrderRequest.setTotalFee(VariableEnum.THE_DEPOSIT.getCode());
                wxPayUnifiedOrderRequest.setRefundDesc("退还预付款");
                wxPayUnifiedOrderRequest.setOutTradeNo(refundId);
                wxPayUnifiedOrderRequest.setNotifyUrl("https://api.bochenkj.cn/pay/notify/refund");
                wxPayUnifiedOrderRequest.setRefundFeeType("CNY");
                wxPayUnifiedOrderRequest.setOutRefundNo(refundId);
                try {
                    wxService.refund(wxPayUnifiedOrderRequest);
                    RedisUtils.del("bed:getRefundId:" + bedOrder.getUserId());
                    RedisUtils.del("bed:getUserId:" + bedOrder.getUserId());
                } catch (WxPayException e) {
                    e.printStackTrace();
                }
            } else {
                bedOrder.setBackPrice(new BigDecimal(0));
                bedOrder.setOrderStatus(2);
                bedOrder.setUnpaidPrice(new BigDecimal(-(double) refundFee / 100));
            }
        }
        bedOrderService.updateById(bedOrder);
        return ResponseFormat.retParam(200, malfunctionService.save(malfunction));
    }
}

